/*
 * 文 件 名 : PopupEditViewWindow.java
 * 版    权 : China Machi.Co.,Ltd.Copyright 2009-2011.All rights reserved
 * 描    述 : <描述>
 * 修 改 人 : lixg
 * 修改时间 : 2012-8-20
 * 跟踪单号 : <跟踪单号>
 * 修改单号 : <修改单号>
 * 修改内容 : <修改内容>
 */
package com.fire8.dolphin.ui;


import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.fire8.dolphin.utils.SmileyParser;


/**
 * 表情显示窗口
 * 以表格形式展示表情符号
 *
 * @author  lixg
 * @version [版本号,2012-8-20]
 * @see     [相关类/方法]
 * @since   [产品/模块]
 */
public class PopupFaceViewWindow extends PopupWindow
{
    private final String TAG="dolphin";
    private static PopupFaceViewWindow pw;//弹出窗口
    private Context context;
    Handler mHandler;
    private GridView gridview;
    //private LinearLayout mLayout;//布局，里面放一个文档框和一个按钮
    private ArrayList<HashMap<String, String>> lstImageItem = new ArrayList<HashMap<String, String>>();
    
    private PopupFaceViewWindow(){}
    public static PopupFaceViewWindow getInstall(Context c,Handler handler)
    {
        if(pw==null)
        {
            pw = new PopupFaceViewWindow(c,handler);
        }
        return pw;
    }
    private PopupFaceViewWindow(Context c,Handler handler)
    {
        super(c);
        context = c;
        mHandler = handler;
        LayoutInflater factory = LayoutInflater.from(c);  //加载popWindow的layout  
        final View faceView = factory.inflate(R.layout.face_grid, null); 
        //final View faceView = factory.inflate(R.layout.input_type_text, null); 
        Log.d(TAG,"构造弹出窗口，获取view");
        //super(buttonView,LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
        //pw = new PopupWindow(buttonView,400,400); //请注意，使用此构造方法，一定要指定popupWindow的长宽，之后调用popupWindow的showAXXX方法才会显示指定的contentView
        ColorDrawable cd = new ColorDrawable(com.fire8.dolphin.ui.R.color.white);
        this.setBackgroundDrawable(cd);
        this.setFocusable(true);
        this.setTouchInterceptor(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_OUTSIDE)
                {
                    Log.d(TAG,"按到外面了，关闭窗口");
                    pw.dismiss();
                    return true;
                }
                return false;
            }
        });
        getSmileyData();
//        GridView gvFace = (GridView)faceView.findViewById(R.id.gv_face);
//        Log.d(TAG,"gvFace id:"+gvFace.getId());
//        gvFace.setAdapter(new MyAdapter());
//        //gvFace.setAdapter(initAdapter());
//        Log.d(TAG,"gvFace getCount:"+gvFace.getCount());
//        Log.d(TAG,"gvFace getChildCount:"+gvFace.getChildCount());
//        //faceView.
//        Log.d(TAG,"设置适配器");
        
        createGridview();
        gridview.setAdapter(new GridviewAdapter());
        //faceView.
        
        //设置默认项  
        this.setContentView(gridview);  
        this.setWidth(LayoutParams.FILL_PARENT);  
        this.setHeight(280);
        
        //this.setBackgroundDrawable(new ColorDrawable());// 设置窗口背景  
        //this.setAnimationStyle(aniTabMenu);  
        this.setFocusable(true);// 窗口获得焦点 如果没有获得焦点窗口中的控件事件无法响应  
    }
    
    private void createGridview()
    {
        gridview = new GridView(context);
        //gridview.setBackgroundDrawable(new ColorDrawable(-0000));
        gridview.setBackgroundColor(com.fire8.dolphin.ui.R.color.white);
        gridview.setColumnWidth(55);  
        gridview.setHorizontalSpacing(2); 
        gridview.setVerticalSpacing(2);
        gridview.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);  
        gridview.setNumColumns(GridView.AUTO_FIT);
        gridview.setPadding(1, 1, 1, 1);
        gridview.setOnItemClickListener(new ItemClickListener());
        
    }
    
    private void getSmileyData()
    {
        
        SmileyParser parser = SmileyParser.getInstance(context);
        int[] smileyIDs = parser.getSmileyIDs();
        String[] smileyTexts = parser.getSmileyTexts();
        for (int i=0,len=smileyIDs.length;i<len;i++)
        {
            int id = parser.getSmileyResourceId(smileyIDs[i]);
            if(id != -1)
            {
                HashMap<String, String> map  = new HashMap<String, String>();
                map.put("id", String.valueOf(id));
                map.put("text", String.valueOf(smileyTexts[i]));
                lstImageItem.add(map);
            }
        }
        
    }
    public final class ViewHolder
    {
        
        public ImageView imgFace;
        
    }
    public class GridviewAdapter extends BaseAdapter
    {
        private LayoutInflater mInflater; 
        public GridviewAdapter()
        {
            this.mInflater = LayoutInflater.from(context); 
        }
        
        public int getCount()
        {
            Log.d(TAG,"getCount:"+lstImageItem.size());
            return lstImageItem.size();
        }

        
        public Object getItem(int position)
        {
            return null;
        }

        
        public long getItemId(int position)
        {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            
            //Log.d(TAG,"getView: "+position);
            ViewHolder holder = null; 
            if (convertView == null)
            {
                
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.face_grid_item, null);
                holder.imgFace = (ImageView)convertView.findViewById(R.id.img_face_item);
                //Log.d(TAG,"获得imgFace");
                convertView.setTag(holder);
            }
            else
            {
                holder = (ViewHolder)convertView.getTag();
            }
            HashMap<String, String> map= lstImageItem.get(position);
            
            holder.imgFace.setImageBitmap(BitmapFactory.decodeResource(convertView.getResources(), Integer.valueOf(map.get("id"))));
            holder.imgFace.setTag(map.get("text"));
            //Log.d(TAG,"text: "+map.get("text"));
            return convertView;
        }
        
    }
    
    class ItemClickListener implements OnItemClickListener
    {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3)
        {
            //取得图片对应的text，回传到父页面
            HashMap<String, String> map = lstImageItem.get(position);
            mHandler.obtainMessage(R.id.btn_input_face, map.get("text")).sendToTarget();
            PopupFaceViewWindow.this.dismiss();
        }
        
    }
    
}
